﻿//-----------------------------------------------------------------------
// <copyright file="ServiceCollectionExtensions.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: ServiceCollectionExtensions.cs
// * history : created by qinchaoyue 2017-12-13 04:44:10
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Linq;

namespace Slwy.FrameCore.Dependency
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection Add(this IServiceCollection collection, Type serviceType, Type implementationType, LifetimeStyle lifetime)
        {
            ServiceDescriptor descriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);
            return collection.TryAdd(descriptor);
        }

        public static IServiceCollection TryAdd(this IServiceCollection collection, ServiceDescriptor descriptor)
        {
            if (collection.Any(m => m.ServiceType == descriptor.ServiceType
            && m.ImplementationType == descriptor.ImplementationType))
            {
                return collection;
            }

            collection.Add(descriptor);
            return collection;
        }
    }
}
